package com.tracking.application.fragments;


import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.tracking.application.DataBase.DbAdapter;
import com.tracking.application.R;
import com.tracking.application.activities.TabsActivity;

import java.util.Collections;

/**
 * Created with IntelliJ IDEA.
 * User: Kamil.O
 * Description: Statistic tab activity
 * Date: 18.11.13
 * Time: 22:56
 */
public class StatisticFragment extends Fragment {
    private TabsActivity tabsActivity;
    private DbAdapter db;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            tabsActivity = (TabsActivity) activity;
            db = new DbAdapter(tabsActivity);
        } catch(ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement DataPullingInterface");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.statistic_activity, container, false);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

        double km = stat1();

        stat2();

        long miliseconds = stat3();

        double result = km / (((double)miliseconds/1000)/60)/60;
        TextView stat4 = (TextView) tabsActivity.findViewById(R.id.statValue4);
        stat4.setText(String.valueOf(tabsActivity.setPrecision(result, 2)+" km/h"));

        stat5();

        stat6();

        stat7();

        stat8();

        stat9();

        stat10();

        stat11();

        stat12();
    }

    private void stat5() {
        TextView stat5 = (TextView) tabsActivity.findViewById(R.id.statValue5);
        stat5.setText("---");
    }

    private void stat6() {
        TextView stat6 = (TextView) tabsActivity.findViewById(R.id.statValue6);
        stat6.setText("---");
    }

    private long stat3() {
        DbAdapter db = tabsActivity.getDb();
        db.open();
        long miliseconds = db.getTimeRoad(tabsActivity.getIdRoad());
        db.close();

        String secondsFinal = String.valueOf(miliseconds/1000 % 60);
        String minutes = String.valueOf((miliseconds/1000)/60);
        String hours = String.valueOf(((miliseconds/1000)/60)/60);

        if (secondsFinal.length() <= 1) secondsFinal = "0"+secondsFinal;
        if (minutes.length() <= 1) minutes = "0"+minutes;
        if (hours.length() <= 1) hours = "0"+hours;

        TextView stat3 = (TextView) tabsActivity.findViewById(R.id.statValue3);
        stat3.setText(hours+":"+minutes+":"+secondsFinal);

        return miliseconds;
    }

    private void stat8() {
        TextView stat8 = (TextView) tabsActivity.findViewById(R.id.statValue8);
        stat8.setText("---");
    }

    private void stat11() {
        double temp = 0;
        if (tabsActivity.getAltitudeListPrecision().size() > 0){
            temp = Collections.min(tabsActivity.getAltitudeListPrecision());
        }

        TextView stat11 = (TextView) tabsActivity.findViewById(R.id.statValue11);
        stat11.setText(String.valueOf(tabsActivity.setPrecision(temp, 2))+"m n.p.m.");
    }

    private void stat12() {
        double temp = 0;
        if (tabsActivity.getAltitudeListPrecision().size() > 0){
            temp = Collections.max(tabsActivity.getAltitudeListPrecision());
        }

        TextView stat12 = (TextView) tabsActivity.findViewById(R.id.statValue12);
        stat12.setText(String.valueOf(tabsActivity.setPrecision(temp, 2))+"m n.p.m.");
    }

    private void stat10() {
        double temp = 0;
        if (tabsActivity.getAltitudeListPrecision().size() > 0){
            temp = tabsActivity.getAltitudeListPrecision().get(tabsActivity.getAltitudeListPrecision().size()-1);
        }
        TextView stat10 = (TextView) tabsActivity.findViewById(R.id.statValue10);
        stat10.setText(String.valueOf(tabsActivity.setPrecision(temp, 2))+"m n.p.m.");
    }

    private void stat9() {
        double temp = 0;
        if (tabsActivity.getAltitudeListPrecision().size() > 0){
            temp = tabsActivity.getAltitudeListPrecision().get(0);
        }
        TextView stat9 = (TextView) tabsActivity.findViewById(R.id.statValue9);
        stat9.setText(String.valueOf(tabsActivity.setPrecision(temp, 2))+"m n.p.m.");
    }

    private void stat2() {
        TextView stat2 = (TextView) tabsActivity.findViewById(R.id.statValue2);
        stat2.setText("---");
    }

    private double stat1() {
        double temp = 0;
        if (tabsActivity.getDistanceListSumForGraphKm().size() > 0){
             temp = tabsActivity.getDistanceListSumForGraphKm().get(tabsActivity.getDistanceListSumForGraphKm().size()-1);
        }

        TextView stat1 = (TextView) tabsActivity.findViewById(R.id.statValue1);
        stat1.setText(String.valueOf(tabsActivity.setPrecision(temp, 2))+" km");
        return temp;
    }

    private void stat7() {
        TextView stat7 = (TextView) tabsActivity.findViewById(R.id.statValue7);
        stat7.setText(String.valueOf(tabsActivity.setPrecision(tabsActivity.getMaxSpeed(), 2)+" km/h"));
    }
}
